package com.dtagri.fjwlw.api;

import java.util.Locale;

import org.apache.http.client.params.ClientPNames;

import android.util.Log;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.util.TToast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

/**
 * @author wxjn
 * 创建时间: 2015-
 * 最近修改时间: 2015-11-05
 * 修改人: CL.
 */
public class ApiHttpClient {

	public final static String HOST = "www.fjwlw.org";
	//公司SVN测试 			 27.155.177.209:8010   
	//物联网2.0服务器IP 	 117.27.143.84:80
	public final static String IP = "117.27.143.84:80";		
	public static final String API_URL = "http://" + HOST + "/Tjwlw/api/%s";
//	private static final String API_URL = "http://192.168.16.13:7080/Tjwlw/api/%s";
//	private static final String API_URL = "http://wxjn.name/Tjwlw/api/%s";
	// public final static String HOST = "192.168.16.13";
	// private static String API_URL = "http://192.168.13.13/%s";
	public static AsyncHttpClient client;

	static {
		// 初始化网络请求
		AsyncHttpClient client = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				AppContext.context());
		client.setCookieStore(myCookieStore);
		ApiHttpClient.setHttpClient(client);
	}

	public static AsyncHttpClient getHttpClient() {
		return client;
	}

	public static void get(String partUrl, AsyncHttpResponseHandler handler) {
		client.get(getAbsoluteApiUrl(partUrl), handler);
		log(new StringBuilder("GET ").append(partUrl).toString());
	}

	public static void get(String partUrl, RequestParams params,
			AsyncHttpResponseHandler handler) {
//		System.out.println("---------url:-"+partUrl);
//		System.out.println("---------params:-"+params.toString());
		client.get(getAbsoluteApiUrl(partUrl), params, handler);
		log(new StringBuilder("GET ").append(partUrl).append("&")
				.append(params).toString());
	}

	public static String getAbsoluteApiUrl(String partUrl) {
		String serverIp = "http://" + AppContext.user.getServerIp() + "/Tjwlw/api/%s";
		System.out.println("httpclient-serverIp:"+serverIp);
		String url = String.format(serverIp, partUrl);
//		Log.d("BASE_CLIENT", "request:" + url);
		return url;
	}

	public static void getDirect(String url, AsyncHttpResponseHandler handler) {
		client.get(url, handler);
		log(new StringBuilder("GET ").append(url).toString());
	}

	private static void log(String log) {
		Log.d("BaseApi", log);
	}

	public static void post(String partUrl, RequestParams params,AsyncHttpResponseHandler handler) {
		client.post(getAbsoluteApiUrl(partUrl), params, handler);
		log(new StringBuilder("POST ").append(partUrl).append("&")
				.append(params).toString());
	}	
	/**
	 * 连接鼎天服务器
	 * @param partUrl
	 * @param params
	 * @param handler
	 */
	public static void postServerDT(String partUrl, RequestParams params,AsyncHttpResponseHandler handler) {
		try {
			client.post(getAbsoluteApiUrlServerDT(partUrl), params, handler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("-----------------------");
			e.printStackTrace();
		}
		log(new StringBuilder("POST ").append(partUrl).append("&")
				.append(params).toString());
	}	
	/**
	 * 连接鼎天服务器
	 * @param partUrl
	 * @return
	 */
	public static String getAbsoluteApiUrlServerDT(String partUrl) {
		String url = String.format(API_URL, partUrl);
		Log.d("BASE_CLIENT", "request:" + url);
		return url;
	}
	public static void setHttpClient(AsyncHttpClient c) {
		client = c;
		client.addHeader("Accept-Language", Locale.getDefault().toString());
//		client.addHeader("Host", HOST);
		client.addHeader("Connection", "Keep-Alive");
		client.getHttpClient().getParams()
				.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
	}
}
